package com.neoCalf.neoAuth.config;

import com.neoCalf.neoAuth.properties.AuthProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.annotation.Resource;


/**
 * @author 小牛
 * @version 1.0
 * @description
 * @date 2025/4/7 21:37
 */
@Configuration
@Slf4j
public class WebConfig extends WebMvcConfigurationSupport {
    @Resource
    private AuthProperties authProperties;

    /**
     * 配置跨域
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins(authProperties.getOrigins())
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .allowedHeaders("Content-Type", "Authorization")  // 允许的请求头
                .maxAge(3600)
                .allowCredentials(true);
        log.info("neoCalf认证中心跨域配置完成");
    }

    /**
     * 配置swagger
     */
    @Bean
    public Docket docket() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("neoAuth")
                .version("1.0.0")
                .description("neoCalf认证中心")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.neoCalf.neoAuth.controller"))
                .paths(PathSelectors.any())
                .build();
        log.info("neoCalf认证中心接口文档生成完成");
        return docket;
    }
}
